package cn.lds.healthj377.service.impl;

import cn.lds.healthj377.domain.User;
import cn.lds.healthj377.dto.DsHealthConstant;
import cn.lds.healthj377.dto.ResponseDTO;
import cn.lds.healthj377.exception.SysException;
import cn.lds.healthj377.mapper.UserMapper;
import cn.lds.healthj377.service.UserService;
import cn.lds.healthj377.utils.MD5Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

@Service
public class UserServiceImpl implements UserService {

    private static final Logger log = LoggerFactory.getLogger(UserServiceImpl.class);
    @Autowired
    private UserMapper userMapper;

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    @Override
    public ResponseDTO<List<User>> selectUserAll(User user) throws SysException {

        List<User> users = userMapper.selectUserAll(user);

        if (CollectionUtils.isEmpty(users)){
            throw new SysException(ResponseDTO.buildError(DsHealthConstant.User.QUERY_ERROR));
        }


        return ResponseDTO.buildSuccess(users);
    }

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    @Override
    public ResponseDTO<User> selectUserById(Integer id) throws SysException {


        User user = userMapper.selectUserById(id);
        if (Objects.isNull(user)){
            throw new SysException(ResponseDTO.buildError(DsHealthConstant.User.QUERY_ERROR));
        }

        return ResponseDTO.buildSuccess(user);
    }

    @Override
    public ResponseDTO<User> loginUser(User user) throws SysException {
        //密码解密处理
        user.setPassword(MD5Utils.MD5(user.getPassword()));

        User loginUser = userMapper.loginUser(user);
        if (Objects.isNull(loginUser)){
            return ResponseDTO.buildError(DsHealthConstant.User.LOGIN_ERR);
        }

        return ResponseDTO.buildSuccess(loginUser);
    }

    @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
    @Override
    public ResponseDTO addUser(User user) {



        return null;
    }

    @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
    @Override
    public ResponseDTO regUser(User user) throws SysException {


        //密码加密处理
        user.setPassword(MD5Utils.MD5(user.getPassword()));

        int i = userMapper.regUser(user);

        if (i <=0){
            throw new SysException(ResponseDTO.buildError(DsHealthConstant.SysMsg.ADD_ERR));
        }

        return ResponseDTO.buildSuccess();
    }

    @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
    @Override
    public ResponseDTO modifyUser(User user) throws SysException {
        int i = userMapper.modifyUser(user);

        if (i <=0){
            throw new SysException(ResponseDTO.buildError(DsHealthConstant.SysMsg.ADD_ERR));
        }

        return ResponseDTO.buildSuccess();
    }

    @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
    @Override
    public ResponseDTO removeUserById(Integer id) {
        return null;
    }

    @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
    public void exception() throws SysException {
        throw new SysException(null);
    }

    @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
    @Override
    public ResponseDTO thransfer(Integer fromUserId, Integer toUserId, int many) {

        User fromUser = userMapper.selectUserById(fromUserId);

        fromUser.setMoney(fromUser.getMoney()- many);

        userMapper.modifyUser(fromUser);

        try {
            exception();
        } catch (SysException e) {
            //捕获异常后，抛出运行时异常，保证事务有效
            throw new RuntimeException();

        }
//
        User toUser = userMapper.selectUserById(toUserId);
//
//
        toUser.setMoney(toUser.getMoney() + many);

        userMapper.modifyUser(toUser);


        log.info("fromUser:{}",fromUser);
        log.info("toUser:{}",toUser);


        return null;
    }


}
